#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
#define maxn 10001
#define maxc 50001
const int INF = 1<<30;

int n, c;
int dp[maxn][1 << 6];
int num[maxn][1 << 6];
//num[pos][state]代表pos为起点状态为state的满意个数
int ans=0;

inline bool check(int state, int afraid, int favor){
	if((state & favor) || (~state & afraid)) return true;
	else return false;
}

/*  参数i  搜索到是否移除/保留第i个栅栏
 *  参数state  从i开始后5个栅栏的状态
 *  返回值  移除/不移除/保留/不保留栅栏i所能达到最大满意数
 */
int dfs(int i, int state) {
	if(i >= n) return 0;
	if (dp[i][state] >= 0) return dp[i][state];
	int ret0 = dfs(i + 1, (state & 15) << 1);
	int ret1 = dfs(i + 1, ((state & 15) << 1) | 1);
	return dp[i][state] = (max(ret0, ret1) + num[i][state]);
}

int main() {
    cin >> n >> c;
    for (int i = 0; i < c; i++) {
        int e, f, l;
        cin >> e >> f >> l;
        int afraid=0, favor=0;
        for (int j = 0; j < f; j++) {
            int ff; cin >> ff;
			ff = (ff-e+n)%n;
			ff = 4-ff;
			afraid |= 1<<ff;
        }
        for (int j = 0; j < l; j++) {
            int ll; cin >> ll;
			ll = (ll-e+n)%n;
			ll = 4-ll;
			favor |= 1<<ll;
        }
        for(int state=0; state<(1<<5); state++){
        	if(check(state, afraid, favor))
        		num[e-1][state]++;
        }
    }
    
    memset(dp, -1, sizeof(dp));
    ans = max(ans, dfs(0, 0));
    ans = max(ans, dfs(0, 16));
    cout << ans << endl;

    return 0;
}